package com.kotlin.library.widgets

import android.app.Dialog
import android.content.Context
import android.graphics.drawable.AnimationDrawable
import android.support.annotation.StyleRes
import android.view.Gravity
import android.widget.ImageView
import com.kotlin.library.R

/**
 *
 * USER: zjw
 * DATA: 2018/11/30 0030
 * TIME: 下午 2:14
 */
class ProgressLoading(context: Context, @StyleRes themeResId: Int) : Dialog(context, themeResId) {
    companion object {
        private lateinit var mDialog: ProgressLoading
        private var drawable: AnimationDrawable? = null

        fun create(context: Context): ProgressLoading {
            mDialog = ProgressLoading(context, R.style.LightProgressDialog)
            mDialog.setContentView(R.layout.progress_dialog)
            mDialog.setCanceledOnTouchOutside(false)
            mDialog.setCancelable(true)
            mDialog.window.attributes.gravity = Gravity.CENTER
            //设置背景透明度：
            val attributes = mDialog.window.attributes
            attributes.dimAmount = 0.2f
            mDialog.window.attributes = attributes

            //设置图片的背景动画
            drawable = mDialog.findViewById<ImageView>(R.id.iv_loading).background as AnimationDrawable

            return mDialog
        }
    }

    fun showLoading() {
        //dialog 展示
        super.show()
        drawable?.start()
    }

    fun hidLoading() {
        super.dismiss()
        drawable?.stop()
    }
}